#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author   : Moty
# @Time     : 2024/1/22 0:10
# @File     : jt808_8100.py
# @Project  : jt808
from utils.common.convert import ascii_to_string


def parse_register_response(body):
    # 解析0x8100 终端注册应答
    data = {
        "应答流水号": body[0:4],
        "结果": str(parse_register_result(int(body[4:6], 16)) + '(' + body[4:6]) + ')',
        "鉴权码": ascii_to_string(body[6:]) + '(' + body[6:] + ')',
    }
    formatted_data = ""
    for key, value in data.items():
        formatted_data += f"{key}: {value}\n"
    return formatted_data


def parse_register_result(result_value):
    results = ['成功', '车辆已被注册', '数据库中无该车辆', '终端已被注册', '数据库中无该终端']
    if 0 <= result_value < len(results):
        return results[result_value]
    else:
        return '未知结果'


__all__ = ['parse_register_response']